package com.main;

import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;

//import com.nextwavesoft.LinearSample;
import com.nextwavesoft.enumeration.CircularBackgroundType;
import com.nextwavesoft.enumeration.DigitalCharacterOverflowAlignment;
import com.nextwavesoft.enumeration.DigitalCharacterScrollingState;
import com.nextwavesoft.enumeration.DigitalCharacterType;
import com.nextwavesoft.enumeration.LedState;
import com.nextwavesoft.enumeration.PointerBarType;
import com.nextwavesoft.enumeration.PointerCapType;
import com.nextwavesoft.enumeration.RectangularFrameType;
import com.nextwavesoft.enumeration.ScalePlacement;
import com.nextwavesoft.enumeration.TextOrientation;
import com.nextwavesoft.enumeration.UnitType;
import com.nextwavesoft.gauge.*;
import com.nextwavesoft.primitives.GaugeItem;
import com.nextwavesoft.shared.Unit;

public class DashBoard extends JFrame{
	int FRAME_WIDTH = 500, FRAME_HEIGHT = 250;
	
	
	CircularPointerNeedle speedomainneedle = new CircularPointerNeedle();
	CircularPointerNeedle speedosubneedle = new CircularPointerNeedle();
	
	LinearPointerBar bar = new LinearPointerBar();
	DigitalGauge gg = null;
	
	JPanel tempPanel;
	JPanel speedPanel;
	
	public DashBoard(){
		// window
		Dimension stdout = Toolkit.getDefaultToolkit().getScreenSize();
		setTitle( "FingerDriver Dashboard" );
		setLocation( ( int )( stdout.getWidth() - FRAME_WIDTH ), 0 );
		
		// appearance
		setBackground( Color.BLACK );
		// Speed meter
		speedPanel = new JPanel();
		createAndShowSpeed();
		
		// Direction N - E - W - S (?)
		// temperature
		tempPanel = new JPanel();
		createAndShowTemp();
		
		// layout
		setLayout( new GridLayout( 0, 2 ) );
		add( speedPanel );
		add( tempPanel );
		setSize( FRAME_WIDTH, FRAME_HEIGHT );
		
		setVisible( true );
	}
	
	// accessor + mutator
	public void setTemp( double temp ){
		bar.setValue( temp );
		gg.setText( Double.toHexString( temp ) );
	}
	
	public void setSpeed( double speed ){
		speedomainneedle.setValue( speed * 10 );
	}
	
	
	// speed meter
	private void createAndShowSpeed(){
		//Create circular gauge which holds circular scale.
		CircularGauge circularGauge = new CircularGauge();
		//circularGauge.set
		circularGauge.setBackground(new Color(44,44,44));
		circularGauge.setRimBrush(new Color(44,44,44));
		circularGauge.setBackgroundType(CircularBackgroundType.CircularTopGradient);
		circularGauge.setBackgroundRadiusRatio(0.97);
		
		/*BufferedImage img = null;
		try {
		    img = ImageIO.read(new File("C:\\Users\\sleong.low\\Documents\\bomb.png"));
		} catch (IOException e) {
			System.out.println(e);
		}*/

		ImageIcon c = new ImageIcon("small-empty-icon.png");
		JLabel imageLabel = new JLabel(c);
		
		GaugeItem labelItem0 = new GaugeItem(imageLabel, 
				new Unit(0,UnitType.Percentage), 
				new Unit(60,UnitType.Percentage), 
				new Unit(0,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		JLabel label1 = new JLabel("Speedometer");
		label1.setForeground(Color.white);
		label1.setFont(new Font("Verdana",Font.BOLD,12));
		GaugeItem labelItem = new GaugeItem(label1, 
				new Unit(0,UnitType.Percentage), 
				new Unit(-20,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		JLabel label2 = new JLabel("km/h");
		label2.setForeground(Color.white);
		label2.setFont(new Font("Verdana",Font.PLAIN,10));
		GaugeItem labelItem1 = new GaugeItem(label2, 
				new Unit(30,UnitType.Percentage), 
				new Unit(50,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		JLabel label3 = new JLabel("MPH");
		label3.setForeground(Color.white);
		label3.setFont(new Font("Verdana",Font.PLAIN,10));
		GaugeItem labelItem3 = new GaugeItem(label3, 
				new Unit(30,UnitType.Percentage), 
				new Unit(58,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		
		/*CircularGauge innergauge =  setupInner();
		GaugeItem labelItem4 = new GaugeItem(innergauge, 
				new Unit(0,UnitType.Percentage), 
				new Unit(58,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage));*/
		
		
		circularGauge.addGaugeItem(labelItem);
		circularGauge.addGaugeItem(labelItem1);
		circularGauge.addGaugeItem(labelItem3);
		circularGauge.addGaugeItem(labelItem0);
		
		//Create circular scale which holds circular tickset.
		CircularScale circularScale = new CircularScale();
		circularScale.setRadius(new Unit(74,UnitType.Percentage));
		circularScale.setBarExtent(new Unit(2,UnitType.Percentage));
		circularScale.setStartAngle(135);
		circularScale.setSweepAngle(270);
		circularScale.setBarVisible(false);
	
		
		CircularScale circularScaleTemp = new CircularScale();
		circularScaleTemp.setRadius(new Unit(50,UnitType.Percentage));
		circularScaleTemp.setBarExtent(new Unit(2,UnitType.Percentage));
		circularScaleTemp.setStartAngle(50);
		circularScaleTemp.setSweepAngle(80);
		circularScaleTemp.setBackground(Color.GRAY);
		
		
		circularGauge.getCircularScaleCollection().add(circularScale);
		circularGauge.getCircularScaleCollection().add(circularScaleTemp);
		
		
		CircularTickSet circularTickSetMph = new CircularTickSet();
		circularTickSetMph.setMinimum(0);
		circularTickSetMph.setMaximum(305);
		circularTickSetMph.setMinorInterval(0);
		circularTickSetMph.setMajorInterval(20);
		
		
		
		CircularTickLabelMajor tickLabelMajorMph = new CircularTickLabelMajor();
		//@TODO Label Position
		tickLabelMajorMph.setFontFamily("Verdana");
		tickLabelMajorMph.setFontSize(new Unit(5,UnitType.Percentage));
		//@TODO Not Working
		tickLabelMajorMph.setForeground(Color.WHITE);
		tickLabelMajorMph.setOrientation(TextOrientation.Rotated);
		tickLabelMajorMph.setScalePlacement(ScalePlacement.Inside);		
		tickLabelMajorMph.setScaleOffset(new Unit(32,UnitType.Percentage));
		double [] skipDoublesMph = new double[]{305};
		ArrayList<Double> skipValuesMph = new ArrayList<Double>();
		for(double skipDouble : skipDoublesMph )
			skipValuesMph.add(skipDouble);
		tickLabelMajorMph.setSkipValues(skipValuesMph);
		
		//create tick mark major 
		CircularTickMarkMajor tickMarkMajorMph = new CircularTickMarkMajor();
		tickMarkMajorMph.setTickMarkAscent(new Unit(3,UnitType.Percentage));
		tickMarkMajorMph.setTickMarkExtent(new Unit(3,UnitType.Percentage));
		tickMarkMajorMph.setBackground(Color.WHITE);
		tickMarkMajorMph.setBorderWidth(0);
		tickMarkMajorMph.setScaleOffset(new Unit(25,UnitType.Percentage));
		tickMarkMajorMph.setScalePlacement(ScalePlacement.Inside);
		tickMarkMajorMph.setSkipValues(skipValuesMph);
		
		
		circularTickSetMph.getCircularTickCollection().add(tickMarkMajorMph);
		circularTickSetMph.getCircularTickCollection().add(tickLabelMajorMph);
		
		CircularTickSet circularTickSet = new CircularTickSet();
		circularTickSet.setMinimum(0);
		circularTickSet.setMaximum(190);
		circularTickSet.setMinorInterval(2);
		circularTickSet.setMajorInterval(10);

	    //create tick mark minor
		CircularTickMarkMinor tickMarkMinor = new CircularTickMarkMinor();
		tickMarkMinor.setTickMarkAscent(new Unit(0.5,UnitType.Percentage));
		tickMarkMinor.setTickMarkExtent(new Unit(4,UnitType.Percentage));
		tickMarkMinor.setBackground(Color.WHITE);
		tickMarkMinor.setBorderWidth(0);
		tickMarkMinor.setScalePlacement(ScalePlacement.Inside);

		//create tick mark major 
		CircularTickMarkMajor tickMarkMajor = new CircularTickMarkMajor();
		tickMarkMajor.setTickMarkAscent(new Unit(1,UnitType.Percentage));
		tickMarkMajor.setTickMarkExtent(new Unit(6,UnitType.Percentage));
		tickMarkMajor.setBackground(Color.WHITE);
		tickMarkMajor.setBorderWidth(0);
		tickMarkMajor.setScalePlacement(ScalePlacement.Inside);
		
		//create tick mark major 
		CircularTickMarkMajor tickMarkMajor2 = new CircularTickMarkMajor();
		tickMarkMajor2.setTickMarkAscent(new Unit(4,UnitType.Percentage));
		tickMarkMajor2.setTickMarkExtent(new Unit(4,UnitType.Percentage));
		tickMarkMajor2.setBackground(Color.WHITE);
		tickMarkMajor2.setBorderWidth(0);
		tickMarkMajor2.setScalePlacement(ScalePlacement.Outside);
		double [] skipDoubles = new double[]{10,30,50,70,90,110,130,150,170};
		ArrayList<Double> skipValues = new ArrayList<Double>();
		for(double skipDouble : skipDoubles )
			skipValues.add(skipDouble);
		tickMarkMajor2.setSkipValues(skipValues);
		
		//create tick mark major 
		CircularTickMarkMajor tickMarkMajor3 = new CircularTickMarkMajor();
		tickMarkMajor3.setTickMarkAscent(new Unit(4,UnitType.Percentage));
		tickMarkMajor3.setTickMarkExtent(new Unit(4,UnitType.Percentage));
		tickMarkMajor3.setBackground(Color.GRAY);
		tickMarkMajor3.setBorderWidth(0);
		tickMarkMajor3.setScalePlacement(ScalePlacement.Outside);
		double [] skipDoubles1 = new double[]{0,20,40,60,80,100,120,140,160,180,190};
		ArrayList<Double> skipValues1 = new ArrayList<Double>();
		for(double skipDouble : skipDoubles1 )
			skipValues1.add(skipDouble);
		tickMarkMajor3.setSkipValues(skipValues1);
		
		
		CircularTickLabelMajor tickLabelMajor = new CircularTickLabelMajor();
		tickLabelMajor.setFontFamily("Verdana");
		tickLabelMajor.setFontSize(new Unit(10,UnitType.Percentage));
		tickLabelMajor.setForeground(Color.WHITE);
	//	tickLabelMajor.setAngle(60);
		tickLabelMajor.setOrientation(TextOrientation.Normal);
		tickLabelMajor.setScalePlacement(ScalePlacement.Inside);		
		tickLabelMajor.setScaleOffset(new Unit(10,UnitType.Percentage));
		double [] skipDoubles3 = new double[]{10,30,50,70,90,110,130,150,170,190};
		ArrayList<Double> skipValues3 = new ArrayList<Double>();
		for(double skipDouble : skipDoubles3 )
			skipValues.add(skipDouble);
		tickLabelMajor.setSkipValues(skipValues3);
		
	
		
		speedomainneedle.setPointerAscent(new Unit(3,UnitType.Percentage));
		speedomainneedle.setPointerExtent(new Unit(105,UnitType.Percentage));
		
		CircularPointerCap maincap = new CircularPointerCap();
		maincap.setBackground(Color.RED);
		maincap.setPointerExtent(new Unit(25,UnitType.Percentage));
		maincap.setCapType(PointerCapType.CircleConvex);
	
		circularTickSet.getCircularPointerCollection().add(maincap);
		circularTickSet.getCircularPointerCollection().add(speedomainneedle);
	
		circularTickSet.getCircularTickCollection().add(tickMarkMajor);
		circularTickSet.getCircularTickCollection().add(tickMarkMinor);
		circularTickSet.getCircularTickCollection().add(tickLabelMajor);
		circularTickSet.getCircularTickCollection().add(tickMarkMajor2);
		circularTickSet.getCircularTickCollection().add(tickMarkMajor3);
		
		
		circularScale.getCircularTickSetCollection().add(circularTickSet);
		circularScale.getCircularTickSetCollection().add(circularTickSetMph);
		
		//set up the fuel meter
		
		CircularTickSet circularTickSettemp = new CircularTickSet();
		circularTickSettemp.setMinimum(0);
		circularTickSettemp.setMaximum(100);
		circularTickSettemp.setMinorInterval(5);
		circularTickSettemp.setMajorInterval(100);
		
		//create tick mark minor
		CircularTickMarkMinor tickMarkMinorTemp = new CircularTickMarkMinor();
		tickMarkMinorTemp.setTickMarkAscent(new Unit(0.5,UnitType.Percentage));
		tickMarkMinorTemp.setTickMarkExtent(new Unit(4,UnitType.Percentage));
		tickMarkMinorTemp.setBackground(Color.WHITE);
		tickMarkMinorTemp.setScalePlacement(ScalePlacement.Inside);

		CircularTickMarkCustom customTemp1 = new CircularTickMarkCustom();
		customTemp1.setTickMarkAscent(new Unit(3,UnitType.Percentage));
		customTemp1.setTickMarkExtent(new Unit(7,UnitType.Percentage));
		customTemp1.setBackground(Color.WHITE);
		customTemp1.setScalePlacement(ScalePlacement.Inside);
		customTemp1.setValue(0);
		
		CircularTickMarkCustom customTemp2 = new CircularTickMarkCustom();
		customTemp2.setTickMarkAscent(new Unit(3,UnitType.Percentage));
		customTemp2.setTickMarkExtent(new Unit(7,UnitType.Percentage));
		customTemp2.setBackground(Color.RED);
		customTemp2.setScalePlacement(ScalePlacement.Inside);
		customTemp2.setValue(100);
		
		CircularTickMarkCustom customTemp3 = new CircularTickMarkCustom();
		customTemp3.setTickMarkAscent(new Unit(3,UnitType.Percentage));
		customTemp3.setTickMarkExtent(new Unit(7,UnitType.Percentage));
		customTemp3.setBackground(Color.WHITE);
		customTemp3.setScalePlacement(ScalePlacement.Inside);
		customTemp3.setValue(25);
		
		CircularTickMarkCustom customTemp4 = new CircularTickMarkCustom();
		customTemp4.setTickMarkAscent(new Unit(3,UnitType.Percentage));
		customTemp4.setTickMarkExtent(new Unit(7,UnitType.Percentage));
		customTemp4.setBackground(Color.WHITE);
		customTemp4.setScalePlacement(ScalePlacement.Inside);
		customTemp4.setValue(50);
		
		CircularTickMarkCustom customTemp5 = new CircularTickMarkCustom();
		customTemp5.setTickMarkAscent(new Unit(3,UnitType.Percentage));
		customTemp5.setTickMarkExtent(new Unit(7,UnitType.Percentage));
		customTemp5.setBackground(Color.WHITE);
		customTemp5.setScalePlacement(ScalePlacement.Inside);
		customTemp5.setValue(75);
		
		CircularTickLabelCustom LabelTemp1 = new CircularTickLabelCustom();
		LabelTemp1.setScaleOffset(new Unit(5,UnitType.Percentage));
		LabelTemp1.setForeground(Color.WHITE);
		LabelTemp1.setScalePlacement(ScalePlacement.Inside);
		LabelTemp1.setOrientation(TextOrientation.RotatedAdjusted);
		LabelTemp1.setFontSize(new Unit(14,UnitType.Percentage));
		LabelTemp1.setText("E");
		LabelTemp1.setValue(0);
		
		CircularTickLabelCustom LabelTemp2 = new CircularTickLabelCustom();
		LabelTemp2.setScaleOffset(new Unit(5,UnitType.Percentage));
		LabelTemp2.setForeground(Color.WHITE);
		LabelTemp2.setScalePlacement(ScalePlacement.Inside);
		LabelTemp2.setOrientation(TextOrientation.RotatedAdjusted);
		LabelTemp2.setFontSize(new Unit(14,UnitType.Percentage));
		LabelTemp2.setText("F");
		LabelTemp2.setValue(100);
		
		speedosubneedle.setPointerAscent(new Unit(3,UnitType.Percentage));
		speedosubneedle.setPointerExtent(new Unit(70,UnitType.Percentage));
		
		CircularPointerCap capTemp = new CircularPointerCap();
		capTemp.setPointerExtent(new Unit(10,UnitType.Percentage));
		capTemp.setCapType(PointerCapType.CircleConvex);
		
		circularTickSettemp.getCircularPointerCollection().add(capTemp);
		circularTickSettemp.getCircularPointerCollection().add(speedosubneedle);
		circularTickSettemp.getCircularTickCollection().add(tickMarkMinorTemp);
		circularTickSettemp.getCircularTickCollection().add(customTemp1);
		circularTickSettemp.getCircularTickCollection().add(customTemp2);
		circularTickSettemp.getCircularTickCollection().add(customTemp3);
		circularTickSettemp.getCircularTickCollection().add(customTemp4);
		circularTickSettemp.getCircularTickCollection().add(customTemp5);
		circularTickSettemp.getCircularTickCollection().add(LabelTemp1);
		circularTickSettemp.getCircularTickCollection().add(LabelTemp2);
		
		circularScaleTemp.getCircularTickSetCollection().add(circularTickSettemp);
		circularScaleTemp.setYDistanceFromCenter(new Unit(60,UnitType.Percentage));
		
		circularGauge.setPreferredSize( new Dimension( 200, 200 ) );
		speedPanel.add( circularGauge );
		
	}
	
	// temperature
	private void createAndShowTemp(){
		
		LinearGauge  linearGauge = new LinearGauge();
		linearGauge.setBackground(Color.BLACK);
		linearGauge.setFrameType( RectangularFrameType.RoundedSideThinRim);
		
		ImageIcon c = new ImageIcon("small-empty-icon.png");
		JLabel imageLabel = new JLabel(c);
		
		GaugeItem labelItem0 = new GaugeItem(imageLabel, 
				new Unit(60,UnitType.Percentage), 
				new Unit(60,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		JLabel label1 = new JLabel("Thermometer");
		label1.setForeground(Color.white);
		label1.setFont(new Font("Verdana",Font.BOLD,12));
		GaugeItem labelItem = new GaugeItem(label1, 
				new Unit(0,UnitType.Percentage), 
				new Unit(60,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(10,UnitType.Percentage));
		
		linearGauge.addGaugeItem(labelItem0);
		linearGauge.addGaugeItem(labelItem);

		//Setup scale bar
		LinearScale linearScale = new LinearScale();
		linearScale.setBackground(Color.WHITE);
		linearScale.setBarExtent(new Unit(80,UnitType.Percentage));
		linearGauge.getLinearScaleCollection().add(linearScale);

		//gaugeLabel
		/*GaugeLabel gLabel = new GaugeLabel();
		gLabel.setText("Nextwave Gauge for Java");
		gLabel.setTopLeftX(new Unit(50,UnitType.Percentage));
		gLabel.setTopLeftY(new Unit(10,UnitType.Percentage));

		ts.gaugeLabels().add(gLabel);*/

		//Setup Major Tick Mark and LAbel 
		LinearTickMarkMajor major = new LinearTickMarkMajor();
		//major.setMajorInterval(20);
		
		LinearTickLabelMajor label = new LinearTickLabelMajor();
		label.setFontSize(new Unit(7,UnitType.Percentage));
		label.setScalePlacement(ScalePlacement.Outside);
		label.setScaleOffset(new Unit(10,UnitType.Percentage));
		label.setMajorInterval(20);
		label.setForeground(Color.WHITE);
		label.setTextFormat("%sC");

		//Setup Pointer Bar
		bar.setBarType(PointerBarType.CircleBulbRoundedRectangle);
                bar.setAnimationEnabled(true);

		//Setup Minor Tick Mark
		LinearTickMarkMinor minor = new LinearTickMarkMinor();

		//Setup Tick Set
		LinearTickSet tickSet = new LinearTickSet();
		tickSet.getLinearPointerCollection().add(bar);
		tickSet.getLinearTickCollection().add(minor);
		tickSet.getLinearTickCollection().add(major);
		tickSet.getLinearTickCollection().add(label);
		
		//Add custom label for farenheight
		for ( int i = 0 ; i <= 10 ; i ++ )
		{
			LinearTickLabelCustom custom = new LinearTickLabelCustom();
			custom.setFontSize(new Unit(7,UnitType.Percentage));
			custom.setScalePlacement(ScalePlacement.Inside);
			custom.setScaleOffset(new Unit(10,UnitType.Percentage));
			custom.setMajorInterval(20);
			custom.setForeground(Color.WHITE);
			custom.setValue(-100+(i*20));
			custom.setText(Integer.toString((-100+(i*20))*(9/5)+32) + "F");
			tickSet.getLinearTickCollection().add(custom);
		}
		tickSet.setMinimum(-100);
		tickSet.setMaximum(100);

		//add set to scale bar
		linearScale.getLinearTickSetCollection().add(tickSet);
linearScale.setYDistanceFromCenter(new Unit(-20,UnitType.Percentage));
		
        gg = createInnerGauge();
		GaugeItem labelItem3 = new GaugeItem(gg, 
				new Unit(0,UnitType.Percentage), 
				new Unit(15,UnitType.Percentage), 
				new Unit(40,UnitType.Percentage), 
				new Unit(17,UnitType.Percentage));
		linearGauge.addGaugeItem(labelItem3);
		
		//getContentPane().add(linearGauge, BorderLayout.CENTER);
		//setSize( new Dimension( 450, 300 ) );
		
		linearGauge.setPreferredSize( new Dimension( 250, 100 ) );
		tempPanel.add( linearGauge );
		
	}
	
	public DigitalGauge createInnerGauge()
	{
		DigitalGauge digitalGauge = new DigitalGauge();
		digitalGauge.setFrameType( RectangularFrameType.None);
		digitalGauge.setBackground(Color.BLACK);
		digitalGauge.setCharacterType(DigitalCharacterType.DotMatrix5By7Ellipse);
		digitalGauge.setText("Hello");
		digitalGauge.setForeground(Color.WHITE);
		digitalGauge.setCharacterSpacing(1);
		digitalGauge.setCharacterCount(10);
		digitalGauge.setCharacterPadding(2);
		digitalGauge.setCharacterOverflowAlignment(
			DigitalCharacterOverflowAlignment.Left);
		digitalGauge.setScrollState(DigitalCharacterScrollingState.None);
		digitalGauge.setItalic(true);
		digitalGauge.setLedState(LedState.Blinking);
		digitalGauge.setCharacterWidthRatio(0.5);
		return digitalGauge;
	}
	
	// airbag using glass panel
}
